/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sioc.tag;

import com.jspx.scriptmark.XmlEngine;
import com.jspx.sioc.Sioc;
import com.jspx.utils.StringUtil;
import com.jspx.utils.XMLUtil;
import com.jspx.scriptmark.core.TagNode;
import com.jspx.scriptmark.parse.XmlEngineImpl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.SAXException;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-2-12
 * Time: 1:10:34
 */

public class BeanElement extends IocTagNode
{
    public final static String tagName = "bean";
    private final static String key_caption = "caption";
    private final static String key_alias = "alias";
    private final static String key_create = "create";
    private final static String key_singleton = "singleton";
    private final static String key_destroy = "destroy";
    private final static String key_init = "init";

    public BeanElement()
    {

    }

    //真实所在的命名空间,在扫描后才载入,因为存在继承关系，所以要在这里标明，不然会出现两个单引用对象
    private String namespace = Sioc.global;
    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getCaption() throws IOException, SAXException
    {
        return XMLUtil.deleteQuote(getAttribute(key_caption));
    }

    public String[] getAliases() throws IOException, SAXException
    {
        String value = XMLUtil.deleteQuote(getAttribute(key_alias));
        if (!StringUtil.hasLength(value)) return null;
        return StringUtil.split(value, StringUtil.SEMICOLON);
    }

    public String getCreate() throws IOException, SAXException
    {
        return XMLUtil.deleteQuote(getAttribute(key_create));
    }

    public boolean isSingleton() throws IOException, SAXException
    {
        String singleton =  XMLUtil.deleteQuote(getAttribute(key_singleton));
        return StringUtil.isNULL(singleton) || StringUtil.toBoolean(singleton);
    }

    public String getClassName() throws IOException, SAXException
    {
        return XMLUtil.deleteQuote(getAttribute(Sioc.IocClass));
    }

    public String getDestroyMethod() throws IOException, SAXException
    {
        return XMLUtil.deleteQuote(getAttribute(key_destroy));
    }

    public String getInitMethod() throws IOException, SAXException
    {
        return XMLUtil.deleteQuote(getAttribute(key_init));
    }

    public List<PropertyElement> getPropertyElements() throws Exception
    {
        ReadProperty readProperty = new ReadProperty();
        if (XMLUtil.parseXmlString(readProperty,getSource()))
            return readProperty.getPropertyElements();
        return new ArrayList<PropertyElement>();
    }

    public List<TagNode> getMapElements() throws Exception
    {
        XmlEngine xmlEngine = new XmlEngineImpl();
        xmlEngine.putTag(MapElement.tag_name,MapElement.class.getName());
        return xmlEngine.getTagNodes(getBody());
    }

    public List<TagNode> getListElements() throws Exception
    {
        XmlEngine xmlEngine = new XmlEngineImpl();
        xmlEngine.putTag(ListElement.tag_name,ListElement.class.getName());
        return xmlEngine.getTagNodes(getBody());
    }

    public List<TagNode> getArrayElements() throws Exception
    {
        XmlEngine xmlEngine = new XmlEngineImpl();
        xmlEngine.putTag(ArrayElement.tag_name,ArrayElement.class.getName());
        return xmlEngine.getTagNodes(getBody());
    }
}